package com.aaa.marketresearch.android.data.domain;

import org.json.simple.JSONObject;

import java.io.Serializable;

/**
 * @author Nail
 */
public class Offer implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer id;

    private Long barcode;

    private String name;

    private String description;

    private Boolean countable;

    private String units;

    private Double capacity;

    private Integer demand;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Long getBarcode() {
        return barcode;
    }

    public void setBarcode(Long barcode) {
        this.barcode = barcode;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Boolean getCountable() {
        return countable;
    }

    public void setCountable(Boolean countable) {
        this.countable = countable;
    }

    public String getUnits() {
        return units;
    }

    public void setUnits(String units) {
        this.units = units;
    }

    public Double getCapacity() {
        return capacity;
    }

    public void setCapacity(Double capacity) {
        this.capacity = capacity;
    }

    public Integer getDemand() {
        return demand;
    }

    public void setDemand(Integer demand) {
        this.demand = demand;
    }

    public String printJSON() {
        StringBuilder builder = new StringBuilder();
        builder.append("{\"id\":").append(this.id).append(",");
        builder.append("\"barcode\":").append(this.barcode).append(",");
        builder.append("\"name\":\"").append(this.name).append("\",");
        builder.append("\"description\":\"").append(this.description).append("\",");
        builder.append("\"countable\":").append(countable.toString()).append(",");
        builder.append("\"units\":\"").append(this.units).append("\",");
        builder.append("\"capacity\":").append(this.capacity).append(",");
        builder.append("\"demand\":").append(this.demand).append("}");
        return builder.toString();
    }

    public void setWithJSONObject(JSONObject jObj) {
        Object obj;
        obj = jObj.get("id");
        if (obj != null) {
            this.id = ((Long)obj).intValue();
        }
        obj = jObj.get("barcode");
        if (obj != null) {
            if (obj instanceof Long) {
                this.barcode = (Long) obj;
            } else {
                this.barcode = 0L;
            }
        }
        obj = jObj.get("name");
        if (obj != null) {
            this.name = (String) obj;
        }
        obj = jObj.get("description");
        if (obj != null) {
            this.description = (String) obj;
        }
        obj = jObj.get("countable");
        if (obj != null) {
            this.countable = ((Boolean) obj).booleanValue();
        }
        obj = jObj.get("units");
        if (obj != null) {
            this.units = (String) obj;
            obj = jObj.get("capacity");
            if (obj != null) {
                if (obj instanceof Long) {
                    this.capacity = ((Long) obj).doubleValue();
                } else if (obj instanceof Double) {
                    this.capacity = ((Double) obj).doubleValue();
                } else {
                    this.capacity = 0.0;
                }
            }
        }
        obj = jObj.get("demand");
        if (obj != null) {
            if (obj instanceof Long) {
                this.demand = ((Long) obj).intValue();
            } else {
                this.demand = 0;
            }
        }
    }

    public Offer() {
        this.name = "";
        this.description = "";
        this.barcode = 0L;
        this.countable = true;
        this.units = "PIECE";
        this.capacity = 0.0;
        this.demand = 0;
    }

}
